<?php

namespace App\Service;

use App\Models\GoodsSeckill;
use Illuminate\Http\Request;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\Facades\Log;

class EsasticSearch
{
    //
    /**
     *  添加索引
     * @return string
     * composer require elasticsearch/elasticsearch
     * 高亮
     * <rich-text nodes="{{item}}"></rich-text>
     */
    public static function createDatabase($index, $searchWord)//索引名称，搜索字段
    {
        //本地调用实例化
        $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        //创建索引
        $params = [
            'index' => $index,//数据库名 更换成你自己所创建的索引
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,//创建后可以更改
                    'number_of_replicas' => 1//创建后不可以更改
                ],
                'mappings' => [
                    '_doc' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        'properties' => [
                            //搜索的字段名
                            $searchWord => [
                                'type' => 'text',
                                'analyzer' => 'ik_max_word',//ik分词器
                                'search_analyzer' => 'ik_max_word'
                            ]
                        ]
                    ]
                ]
            ]
        ];
        //将索引添加到kibana中
        $response = $client->index($params);
    }

    /**
     *  添加数据到kibana中
     * @return string
     */
    public static function createData($index, $data)//索引名称。要添加的数据
    {
        //本地调用实例化
        $hosts = [
            '127.0.0.1:9200'
        ];
        //请求连接
        $client = ClientBuilder::create()->setHosts($hosts)->build();
        //查询数据表所有的数据并转化数组格式
        //循环
        foreach ($data as $v) {
            $data = [
                'index' => $index,//更改索引名（数据库名） 其余不变
                'type' => '_doc',
                'id' => $v['id'],
                'body' => $v,
            ];
            $response = $client->index($data);
        }
        //判断 是否添加数据成功
    }

    /**
     *  执行搜索
     * @param Request $request
     */
    public static function search($index,$searchWord,$word)//索引名称，要搜索的字段
    {
        if (!$word)return CommonService::fail(9999,'未检测到值');
        //请求连接
        $client = ClientBuilder::create()->build();
        //搜索的值和库中的值进行对比
        $params = [
            'index' => $index,//数据库名 更换成你自己所创建的索引
            'type' => '_doc',
            'body' => [
                'query' => [
                    'match' => [
                        //要搜索的字段 替换成你自己所要查询额标签
                        $searchWord => $word//要搜索的内容
                    ]
                ],
                'highlight' => [
                    'pre_tags' => ["<em style='color: #ff0000'>"],//样式
                    'post_tags' => ["</em>"],
                    'fields' => [
                        //要搜索的字段 替换成你自己所要查询额标签
                        $searchWord => new \stdClass()
                    ]
                ]
            ],
        ];
        //执行搜索
        $results = $client->search($params);

//        dd($results);
        //循环取出值
        foreach ($results['hits']['hits'] as &$v){
            //title这里的是需要替换成你自己要查询的标签

            $v['_source'][$searchWord] = $v['highlight'][$searchWord][0];
        }
        //替换
        $response =array_column($results['hits']['hits'],'_source');
        return [
            'code' => 200,
            'msg' => '成功',
            'data' => $response
        ];
    }
}
